// const express = require('express');
const db = require("../core/mysql");
const moment = require("moment");
class CartsController {
    //购物车 添加购物车
    async addCart(request, response, next) {
        // console.log("addcard");
        try {
            // 拿到用户id
            // console.log(request.Info.u_id);
            let queryParams = [

                request.Info.u_id,
                request.body.pid,
            ]
            // 根据用户的id和 产品的id 去数据库里查询一次,如果有就修改,没有就新增
            let query = " select * from carts where u_id=? and p_id=? ";
            let queryResult = await db.exec(query, queryParams);

            // 判断是否已经购买过?
            if (queryResult.length >= 1) {
                // 已经购买
                let updateParams = [
                    Number(request.body.pnumber),
                    request.Info.u_id,
                    request.body.pid
                ]
                let updateSQL = " update carts set p_number=p_number+? ,p_total=p_number*p_price where u_id=? and p_id=? ";

                let updateResult = await db.exec(updateSQL, updateParams);
                if (updateResult && updateResult.affectedRows >= 1) {
                    response.json({
                        code: 200,
                        msg: "加入购物车成功",
                    })
                } else {
                    response.json({
                        code: -200,
                        msg: "加入购物车失败",
                    })
                }
            } else {
                // 还没购买
                let insertParams = [
                    request.Info.u_id,
                    request.body.pid,
                    request.body.pname,
                    request.body.pimg,
                    request.body.pnumber,
                    request.body.pprice,
                    request.body.pprice * request.body.pnumber,
                    moment().format("YYYY-MM-DD HH:mm:ss")
                ]

                // console.log(insertParams);
                let insertSQL = " insert into carts (u_id,p_id,p_name,p_img,p_number,p_price,p_total,p_create) "
                insertSQL += " values (?,?,?,?,?,?,?,?) "

                let insertResult = await db.exec(insertSQL, insertParams);
                if (insertResult && insertResult.affectedRows >= 1) {
                    response.json({
                        code: 200,
                        msg: "加入购物车成功",
                    })
                } else {
                    response.json({
                        code: -200,
                        msg: "加入购物车失败",
                    })
                }
            }


        } catch (error) {
            response.json({
                code: -200,
                msg: "加入购物车失败",
                error
            })
        }

        // 获取当前用户
        // 获取当前商品
        // 添加到购物车
    }
    // 获取当前用户的购车
    async getCartByUser(request, response, next) {
        //1.得到当前的用户id
        let params = [
            request.Info.u_id]
        let sql = "select * from carts where u_id=? and p_status=1 ;"


        try {
            let result = await db.exec(sql, params)
            response.json({
                code: 200,
                msg: "获取购物车成功",
                data: result
            })
        } catch (error) {
            response.json({
                code: -200,
                msg: "获取购物车失败",
                error
            })
        }
        // 获取当前用户的购物车
    }


    // 修改购物车
    // 修改数量
    async modifyCart(request, response, next) {
        let params = [
            request.body.pnumber,
            request.body.cid
        ]
        let sql = "update carts set p_number=? ,p_total=p_number * p_price where c_id=? ;"


        try {
           
        let result = await db.exec(sql, params)
        if(result && result.affectedRows>=1){
            response.json({
                code: 200,
                msg: "修改购物车成功"
            })
        }else{
            response.json({
                code: -200,
                msg: "修改购物车失败"
            })
        }
        } catch (error) {
            console.log(error);
            response.json({
                code: -200,
                error   //error之后在后台的控制台中输出，不能带到前端，测试而已
            })
        }
    }


    
    // 删除购物车
    async deleteCart(request, response, next) {
        response.sent("addCard")

    }

    // 结算 把状态1 改成2
    async giveMoney(request, response, next) {
        response.sent("addCard")

    }
}

module.exports = new CartsController();